/*
 Copyright 2014 Alexander V. Nikulin
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
	
 	http://www.apache.org/licenses/LICENSE-2.0
	
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/
package ru.avn.adf;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author Alexander V. Nikulin
 */
public class PayloadDeclarations {

	private PayloadDeclarations() {}
	
	public static <A extends Annotation> PayloadDeclaration<A> newPayloadDeclaration(final A bindedAnnotation, final Field field) {
		return new PayloadDeclaration<A>() {
	
			@Override
			public A getAnnotation() {
				return bindedAnnotation;
			}

			@Override
			public boolean isMethod() {
				return false;
			}
			
			@Override
			public boolean isField() {
				return true;
			}
			
			@Override
			public boolean isClass() {
				return false;
			}
			
			@Override
			public String getName() {
				return field.getName();
			}
						
			@Override
			public Class<?> getType() {
				return field.getType();
			}
			
			@Override
			public Class<?> getContainerType() {
				return field.getDeclaringClass();
			}

			@Override
			public <A1 extends Annotation> A1 getAnnotation(Class<A1> annotationType) {
				return field.getAnnotation(annotationType);
			}
		};
	}
	
	
	public static <A extends Annotation> PayloadDeclaration<A> newPayloadDeclaration(final A bindedAnnotation, final Method method) {
		return new PayloadDeclaration<A>() {
			
			@Override
			public A getAnnotation() {
				return bindedAnnotation;
			}

			
			@Override
			public boolean isMethod() {
				return true;
			}
			
			@Override
			public boolean isField() {
				return false;
			}
			
			@Override
			public boolean isClass() {
				return false;
			}
			
			@Override
			public String getName() {
				return ReflectionUtils.getPropertyName(method);
			}
						
			@Override
			public Class<?> getType() {
				return method.getReturnType();
			}

			@Override
			public Class<?> getContainerType() {
				return method.getDeclaringClass();
			}
			
			@Override
			public <A1 extends Annotation> A1 getAnnotation(Class<A1> annotationType) {
				return method.getAnnotation(annotationType);
			}
		};
	}
	
	
	public static <A extends Annotation> PayloadDeclaration<A> newPayloadDeclaration(final A bindedAnnotation, final Class<?> classType) {
		return new PayloadDeclaration<A> () {
			
			@Override
			public A getAnnotation() {
				return bindedAnnotation;
			}

			
			@Override
			public boolean isMethod() {
				return false;
			}
			
			@Override
			public boolean isField() {
				return false;
			}
			
			@Override
			public boolean isClass() {
				return true;
			}
			
			@Override
			public String getName() {
				return classType.getSimpleName();
			}
						
			@Override
			public Class<?> getType() {
				return classType;
			}

			@Override
			public Class<?> getContainerType() {
				return classType.getSuperclass();
			}
			
			@Override
			public <A1 extends Annotation> A1 getAnnotation(Class<A1> annotationType) {
				return classType.getAnnotation(annotationType);
			}
		};
	}
	
}
